﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace XBLIGUK2011Talk2.HUD
{
    public class HUDButton : HUDObject
    {
        public enum States
        {
            Up,
            Down
        }

        public string Text;

        public States CurrentState = States.Up;

        public Color Shadow = Color.Black;
        SpriteFont font;

        public HUDButton(string name, string text, int top, int left)
            : base(name, top, left)
        {
            Top = top;
            Left = left;

            Text = text;

            Shadow.A = (byte)(255 * .25f);
        }

        public override void Update(GameTime gameTime)
        {
            if (clicked)
                CurrentState = States.Down;
            else
                CurrentState = States.Up;

            bounds = new Rectangle(Left, Top + (int)Parent.Top, (int)font.MeasureString(Text).X + 42, 32);
            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            if (font == null)
                font = Game.Content.Load<SpriteFont>("Fonts/hudFont");

            int strWidth  = (int)font.MeasureString(Text).X;
            int width = strWidth + 32;
            int height = (int)font.MeasureString(Text).Y + 16;
            

            if (CurrentState == States.Up)
                spriteBatch.Draw(Game.Content.Load<Texture2D>("Textures/HUD/HUDButtonUp"), new Rectangle(Left, Top + (int)Parent.Top, width, height), Color.White);
            else
                spriteBatch.Draw(Game.Content.Load<Texture2D>("Textures/HUD/HUDButtonDown"), new Rectangle(Left, Top + (int)Parent.Top, width, height), Color.White);

            if (CurrentState == States.Up)
            {
                spriteBatch.DrawString(font, Text, new Vector2(Left + ((width / 2) - (strWidth / 2)) - 2, Top + (int)Parent.Top + (font.LineSpacing / 2) + 2), Shadow);
                spriteBatch.DrawString(font, Text, new Vector2(Left + (width / 2) - (strWidth / 2), Top + (int)Parent.Top + (font.LineSpacing / 2)), Color.White);
            }
            else
            {
                spriteBatch.DrawString(font, Text, new Vector2(Left + ((width / 2) - (strWidth / 2)) - 4, Top + (int)Parent.Top + (font.LineSpacing / 2) + 2), Shadow);
                spriteBatch.DrawString(font, Text, new Vector2(Left + ((width / 2) - (strWidth / 2)) - 2, Top + (int)Parent.Top + (font.LineSpacing / 2)), Color.White);
            }
        }
    }
}
